package com.lu.common.utils;

import com.lu.system.user.domain.Tree;

import java.util.ArrayList;
import java.util.List;

public class BuildTree {

	public static <T> List<Tree<T>> build(List<Tree<T>> nodes, String idParam) {
		if (nodes == null) {
			return null;
		}
		List<Tree<T>> topNodes = new ArrayList<Tree<T>>();

		for (Tree<T> children : nodes) {

			String pid = children.getParentId();
			if (pid == null || idParam.equals(pid)) {
				topNodes.add(children);
				continue;
			}

			for (Tree<T> parent : nodes) {
				String id = parent.getId();
				if (id != null && id.equals(pid)) {
					parent.getList().add(children);
					continue;
				}
			}

		}
		System.out.println(topNodes);
		return topNodes;
	}

}